<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>广告机设备配置</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            background-color: #f3f4f6;
            padding: 1rem;
        }

        .container {
            width: 100%;
            max-width: 400px;
            background: white;
            padding: 2rem;
            border-radius: 0.5rem;
            box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
        }

        .header {
            text-align: center;
            margin-bottom: 2rem;
        }

        .title {
            font-size: 1.5rem;
            font-weight: bold;
            color: #111827;
            margin-bottom: 0.5rem;
        }

        .subtitle {
            font-size: 0.875rem;
            color: #6b7280;
        }

        .form-group {
            margin-bottom: 1.5rem;
        }

        label {
            display: block;
            font-size: 0.875rem;
            font-weight: 500;
            color: #374151;
            margin-bottom: 0.5rem;
        }

        input {
            width: 100%;
            padding: 0.5rem 0.75rem;
            border: 1px solid #d1d5db;
            border-radius: 0.375rem;
            font-size: 0.875rem;
            transition: border-color 0.15s ease-in-out;
            outline: none;
        }

        input:focus {
            border-color: #2563eb;
            box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
        }

        input::placeholder {
            color: #9ca3af;
        }

        input.error {
            border-color: #dc2626;
        }

        .error-message {
            color: #dc2626;
            font-size: 0.75rem;
            margin-top: 0.25rem;
            display: none;
        }

        .error-message.show {
            display: block;
        }

        button {
            width: 100%;
            padding: 0.625rem 1.25rem;
            background-color: #2563eb;
            color: white;
            border: none;
            border-radius: 0.375rem;
            font-size: 0.875rem;
            font-weight: 500;
            cursor: pointer;
            transition: background-color 0.15s ease-in-out;
        }

        button:hover {
            background-color: #1d4ed8;
        }

        button:focus {
            outline: none;
            box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.5);
        }

        @media (max-width: 640px) {
            .container {
                padding: 1.5rem;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1 class="title">广告机设备配置</h1>
            <p class="subtitle">请输入设备的配置信息</p>
        </div>
        
        <form action="/cgi-bin/config.cgi" method="post" id="configForm" onsubmit="return validateForm()">
            <div class="form-group">
                <label for="gatewayIp">网关 IP</label>
                <input 
                    type="text" 
                    id="gatewayIp" 
                    name="ip" 
                    placeholder="请输入网关IP地址" 
                    required
                    oninput="validateIP(this)"
                >
                <div class="error-message" id="ipError">请输入有效的IP地址 (例如: 192.168.1.1)</div>
            </div>

            <div class="form-group">
                <label for="location">广告机位置</label>
                <input 
                    type="text" 
                    id="location" 
                    name="local" 
                    placeholder="请输入广告机位置"
                    required
                >
            </div>

            <button type="submit">提交配置</button>
        </form>
    </div>

    <script>
        function validateIP(input) {
            const ipRegex = /^(\d{1,3}\.){3}\d{1,3}$/;
            const errorElement = document.getElementById('ipError');
            
            // Remove error styles initially
            input.classList.remove('error');
            errorElement.classList.remove('show');

            if (input.value) {
                if (!ipRegex.test(input.value)) {
                    // Invalid format
                    input.classList.add('error');
                    errorElement.classList.add('show');
                    return false;
                } else {
                    // Check if each number is between 0 and 255
                    const parts = input.value.split('.');
                    const isValid = parts.every(part => {
                        const num = parseInt(part, 10);
                        return num >= 0 && num <= 255;
                    });

                    if (!isValid) {
                        input.classList.add('error');
                        errorElement.classList.add('show');
                        return false;
                    }
                }
            }
            return true;
        }

        function validateForm() {
            const ipInput = document.getElementById('gatewayIp');
            return validateIP(ipInput);
        }

        // Add real-time validation
        document.getElementById('gatewayIp').addEventListener('blur', function() {
            validateIP(this);
        });
    </script>
</body>
</html>